Class ServerStAchievements extends SRAchievementsBase;

var AchievementsObject MyAchievementsObject;
var ServerPerksMut MutatorOwner;
var bool bHasChanged,bStatsChecking,bStHasInit,bHadSwitchedVet,bSwitchIsOKNow;

function int GetID()
{
	return MyAchievementsObject.ID;
}

function SetID( int ID )
{
	MyAchievementsObject.ID = ID;
}

function PreBeginPlay()
{
	if( Rep==None )
	{
		Rep = Spawn(Class'ClientProgressRepLink',Owner);
		Rep.AchievementObject = Self;
	}
	Super.PreBeginPlay();
}

function PostBeginPlay()
{
	local Class<SRVeterancy> Vet;
	
//	if( Rep!=None )
//		Rep.SpawnCustomLinks();

//	bStatsReadyNow = !MutatorOwner.bUseRemoteDatabase;
	bStatsReadyNow = true;
	PlayerOwner = KFPlayerController(Owner);
	MyAchievementsObject = MutatorOwner.GetAchievementsForPlayer(PlayerOwner);
//	if( !bStatsReadyNow )
//	{
//		Timer();
//		SetTimer(1+FRand(),true);
//		return;
//	}
	if( MyAchievementsObject!=None )
	{
		RepCopyStats();
		bHasChanged = true;
		CheckAchievements(true);
	}
	else
	{
		CheckAchievements(true);
	}
	SetTimer(0.1,false);
}

final function CheckAchievements( bool bInit )
{
	local byte i,NewLevel;

	if( !bStatsReadyNow )
		return;
	if( bInit )
	{
//		for( i=0; i<Rep.CachePerks.Length; i++ )
//			Rep.CachePerks[i].CurrentLevel = Rep.CachePerks[i].PerkClass.Static.PerkIsAvailable(Rep);
		return;
	}
/*
	for( i=0; i<Rep.CachePerks.Length; i++ )
	{
		if( Rep.CachePerks[i].CurrentLevel<=Rep.MaximumLevel )
		{
			NewLevel = Rep.CachePerks[i].PerkClass.Static.PerkIsAvailable(Rep);
			if( NewLevel>Rep.CachePerks[i].CurrentLevel )
			{
				if( KFPlayerReplicationInfo(PlayerOwner.PlayerReplicationInfo).ClientVeteranSkill==Rep.CachePerks[i].PerkClass )
					KFPlayerReplicationInfo(PlayerOwner.PlayerReplicationInfo).ClientVeteranSkillLevel = NewLevel-1;
				Rep.CachePerks[i].CurrentLevel = NewLevel;
				Rep.ClientPerkLevel(i,NewLevel);
				if( MutatorOwner.bMessageAnyPlayerLevelUp )
					BroadcastLocalizedMessage(Class'KFVetEarnedMessagePL',(NewLevel-1),PlayerOwner.PlayerReplicationInfo,,Rep.CachePerks[i].PerkClass);
			}
		}
	}
*/
}

final function GetData( string D )
{
	bStatsReadyNow = true;
	MyAchievementsObject.SetSaveData(D);
	RepCopyStats();
	bHasChanged = true;
	CheckAchievements(true);

	SetTimer(0.1,false);
}

function Timer()
{
//	if( !bStatsReadyNow )
//	{
//		MutatorOwner.GetRemoteStatsForPlayer(Self);
//		return;
//	}
	if( !bStHasInit )
	{
		if( KFPCServ(PlayerOwner).ServerAchievements!=None && KFPCServ(PlayerOwner).ServerAchievements!=Self )
			KFPCServ(PlayerOwner).ServerAchievements.Destroy();
		KFPCServ(PlayerOwner).ServerAchievements = Self;
		SRPlayerReplicationInfo(KFPCServ(PlayerOwner).PlayerReplicationInfo).ServerAchievements = Self;
		bStHasInit = true;
//		Rep.SendClientPerks();
	}
	if( bStatsChecking )
	{
		bStatsChecking = false;
		CheckAchievements(false);
	}
}

final function RepCopyStats()
{
	Rep.RBurningCrossbowKillsStat = MyAchievementsObject.BurningCrossbowKillsStat;
}

final function DelayedStatCheck()
{
	if( MyAchievementsObject!=None )
		MyAchievementsObject.bStatsChanged = true;
	if( bStatsChecking || !bStatsReadyNow )
		return;
	bStatsChecking = true;
	SetTimer(1,false);
}

function NotifyStatChanged()
{
	bHasChanged = true;
	DelayedStatCheck();
}

function MatchEnded()
{
}

/*
function AddDamageHealed(int Amount, optional bool bMP7MHeal, optional bool bMP5MHeal);
function AddWeldingPoints(int Amount);
function AddShotgunDamage(int Amount);
function AddHeadshotKill(bool bLaserSightedEBRHeadshot);
function AddStalkerKill();
function AddBullpupDamage(int Amount);
function AddMeleeDamage(int Amount);
function AddFlameThrowerDamage(int Amount);
function AddSilentDamage(int Amount);
function WaveEnded();
function AddKill(bool bLaserSightedEBRM14Headshotted, bool bMeleeKill, bool bZEDTimeActive, bool bM4Kill, bool bBenelliKill, bool bRevolverKill, bool bMK23Kill, bool bFNFalKill, bool bBullpupKill);
function AddBloatKill(bool bWithBullpup);
function AddSirenKill(bool bLawRocketImpact);
function AddStalkerKillWithExplosives();
function AddFireAxeKill();
function AddChainsawScrakeKill();

function AddBurningCrossbowKill()
{
	bHasChanged = true;
	Rep.RBurningCrossbowKillsStat++;
	if( MyAchievementsObject!=None )
		MyAchievementsObject.BurningCrossbowKillsStat++;
	DelayedStatCheck();
}

function AddFeedingKill();
function OnGrenadeExploded();
function AddGrenadeKill();
function OnShotHuntingShotgun();
function AddHuntingShotgunKill();
function KilledEnemyWithBloatAcid();
function KilledFleshpound(bool bWithMeleeAttack, bool bWithAA12, bool bWithKnife, bool bWithClaymore);
function AddMedicKnifeKill();
function AddGibKill(bool bWithM79);
function AddFleshpoundGibKill();
function AddSelfHeal();
function AddOnlySurvivorOfWave();
function AddDonatedCash(int Amount);
function AddZedTime(float Amount);
function AddExplosivesDamage(int Amount);
function WonLostGame( bool bDidWin );
function KilledPatriarch(bool bPatriarchHealed, bool bKilledWithLAW, bool bSuicidalDifficulty, bool bOnlyUsedCrossbows, bool bClaymore);
function OnStatsAndAchievementsReady();
function PostNetBeginPlay();
function InitializeSteamStatInt(int Index, int Value);
function SetSteamAchievementCompleted(int Index);
event SetLocalAchievementCompleted(int Index);
function ServerSteamStatsAndAchievementsInitialized();
function UpdateAchievementProgress();
function int GetAchievementCompletedCount();
event OnPerkAvailable();
function WonLongGame(string MapName, float Difficulty);
function AddDemolitionsPipebombKill();
function AddSCARKill();
function AddCrawlerKilledInMidair();
function Killed8ZedsWithGrenade();
function Killed10ZedsWithPipebomb();
function KilledHusk(bool bDamagedFriendly);
function AddMac10BurnDamage(int Amount);
function AddGorefastBackstab();
function ScrakeKilledByFire();
function KilledCrawlerWithCrossbow();
function OnLARReloaded();
function AddStalkerKillWithLAR();
function KilledHuskWithPistol();
function AddDroppedTier3Weapon();
function Survived10SecondsAfterVomit();
function CheckChristmasAchievementsCompleted();
*/
function Destroyed()
{
	local int i;
	local bool bFound;

	if( Rep!=None )
	{
		Rep.Destroy();
		Rep = None;
	}
	if( PlayerOwner!=None && !PlayerOwner.bDeleteMe )
	{
		// Was destroyed mid-game for random reason, respawn.
		bFound = false;
		for(i=0; i<MutatorOwner.PendingPlayers.Length; i++)
		{
			if ( MutatorOwner.PendingPlayers[i] == PlayerOwner )
			{
				bFound = true;
				break;
			}
		}
		if ( !bFound )
		{
			MutatorOwner.PendingPlayers[MutatorOwner.PendingPlayers.Length] = PlayerOwner;
		}
		MutatorOwner.SetTimer(0.1,false);
	}
	Super.Destroyed();
}

defaultproperties
{
}